﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CocisDBUtility;
using System.Data;
using System.Data.SqlClient;
using CocisModel;

namespace CocisDAL
{
    public class TypesDAL
    {
        #region 判断规格指标是否存在
        /// <summary>
        /// 判断规格指标是否存在
        /// </summary>
        /// <param name="names"></param>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public int CheckTypesExsits(string names, int parentId)
        {
            string sql = "select typeId from types where typeName='" + names + "' and parentId='" + parentId + "'";
            int i = 0;
            SqlDataReader sdr = SQLHelper.ExecuteReader(SQLHelper.ConStr, CommandType.Text, sql, null);
            if (sdr.Read())
            {
                i = 1;

            }
            return i;
        }
        #endregion

        #region 获取所有指标信息 填充到TreeView
        /// <summary>
        /// 获取所有指标信息--填充到TreeView
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public List<TypesInfo> GetAllTypesTreeView(int parentId)
        {
            string sql = "select * from types where parentId='" + parentId + "'";
            using (SqlDataReader read = SQLHelper.ExecuteReader(SQLHelper.ConStr, System.Data.CommandType.Text, sql, null))
            {
                List<TypesInfo> list = new List<TypesInfo>();
                while (read.Read())
                {
                    TypesInfo s = new TypesInfo();

                    s.TypeId = read.GetInt32(0);
                    s.TypeName = read.GetString(1);
                    s.ParentId = read.GetInt32(2);
                    
                    list.Add(s);
                }
                return list;
            }

        }
        #endregion

        #region 获取所有规格指标信息
        /// <summary>
        /// 获取所有规格指标信息--返回数据集
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllTypes()
        {
            string sql = "select * from types";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);

        }

        #endregion

        #region 查询规格指标信息---通过规格指标编号
        /// <summary>
        /// 查询部门信息---通过部门编号
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public DataSet GetTypesInfo(int typeId)
        {
            string sql = "select * from types where typeId='" + typeId + "'";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 通过规格项目编号，查询下级规格列表
        /// <summary>
        /// 通过规格项目编号，查询下级规格列表
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public DataSet GetTypeListByParentId(int parentId)
        {
            string sql = "select * from types where parentId='" + parentId + "'";
            return SQLHelper.SqlDataAdapter(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 新增规格指标信息
        /// <summary>
        /// 新增规格指标信息
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public int AddTypes(int parentId, string typeName)
        {
            string sql = "insert into types(typeName,parentId) values('" + typeName + "','" + parentId + "')";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);
        }

        #endregion

        #region 修改规格指标信息
        /// <summary>
        /// 修改规格指标信息
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="typeName"></param>
        /// <param name="parentId"></param>     
        /// <returns></returns>
        public int UpdateType(int typeId, string typeName, int parentId)
        {
            string sql = "update types set typeName='" + typeName + "',parentId='" + parentId + "'  where typeId='" + typeId + "'";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);

        }
        #endregion

        #region 删除规格指标信息
        public int DeleteType(int typeId)
        {
            string sql = "delete from types where typeId='" + typeId + "'";
            return SQLHelper.ExecuteNonQuery(SQLHelper.ConStr, CommandType.Text, sql, null);


        }

        #endregion
    }
}
